package com.rrt.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Util {

	private static final Logger _log = LoggerFactory.getLogger(Util.class);
			
	public static String restUtilGet(String uri, String method, String requestPropertyKey, String requestPropertyValue) throws IOException {
		_log.info("uri:" + uri + " method:" + method);
		try {
			URL url = new URL(uri);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod(method);
			conn.setRequestProperty(requestPropertyKey, requestPropertyValue);
			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
			}
			BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
			String output = "", strLineas = "";
			while ((output = br.readLine()) != null) {
				strLineas += output;
			}
			conn.disconnect();
			return strLineas;
		} catch (MalformedURLException e) {
			_log.error("Error de Mal formato de Url ", e);
			throw new MalformedURLException("Error de Mal formato de Url ");
		}
	}
	private static Properties props = new Properties();
	
	static{
		 props.put("mail.smtp.auth", Config.getPropiedad("mail.smtp.auth"));
		 props.put("mail.smtp.starttls.enable", Config.getPropiedad("mail.smtp.starttls.enable"));
		 props.put("mail.smtp.host", Config.getPropiedad("mail.smtp.host"));
		 props.put("mail.smtp.port", Config.getPropiedad("mail.smtp.port"));
	}
	
	public static void enviarCorreo(String to, String asunto, String html) {
		try {
			
			Session session = Session.getInstance(props,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(
									Config.getPropiedad("rrtMq.correo.username"),
									Config.getPropiedad("rrtMq.correo.password"));
						}
					});

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(Config
					.getPropiedad("rrtMq.correo.from")));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));
			message.setSubject(asunto.toUpperCase());
			message.setText(html);
			Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	// public static void main(String[] args) {
	// // Recipient's email ID needs to be mentioned.
	// String to = "danieldelgado20g@gmail.com";//change accordingly
	//
	// // Sender's email ID needs to be mentioned
	// String from = "luxijavag@gmail.com";//change accordingly
	// final String username = "luxijavag";//change accordingly
	// final String password = "LuxiJava123";//change accordingly
	//
	// // Assuming you are sending email through relay.jangosmtp.net
	// String host = "smtp.gmail.com";
	//
	// Properties props = new Properties();
	// props.put("mail.smtp.auth", "true");
	// props.put("mail.smtp.starttls.enable", "true");
	// props.put("mail.smtp.host", host);
	// props.put("mail.smtp.port", "587");
	//
	// // Get the Session object.
	// Session session = Session.getInstance(props,
	// new javax.mail.Authenticator() {
	// protected PasswordAuthentication getPasswordAuthentication() {
	// return new PasswordAuthentication(username, password);
	// }
	// });
	//
	// try {
	// // Create a default MimeMessage object.
	// Message message = new MimeMessage(session);
	//
	// // Set From: header field of the header.
	// message.setFrom(new InternetAddress(from));
	//
	// // Set To: header field of the header.
	// message.setRecipients(Message.RecipientType.TO,
	// InternetAddress.parse(to));
	//
	// // Set Subject: header field
	// message.setSubject("Testing Subject");
	//
	// // Now set the actual message
	// message.setText("Hello, this is sample for to check send "
	// + "email using JavaMailAPI 2");
	//
	// // Send message
	// Transport.send(message);
	//
	// System.out.println("Sent message successfully....");
	//
	// } catch (MessagingException e) {
	// e.printStackTrace();
	// throw new RuntimeException(e);
	// }
	// }

}
